package com.altran.mirth.migration.v211;

import java.util.ArrayList;

/**
 * Project :  Mirth
 * Author :   Altran
 * URL :      http://Mirth.eryos.fr
 *
 * Copyright (C) 2011 - Altran 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

public class MigrationComment {

	public enum Category{
		BATCH,
		DEPRECATED,
		USERDEFINED;
		
		int id;
		int nbChild = 0;
		int getId(){
			return this.ordinal();
		}
		int addChild(){
			return id*10+nbChild++;
		}
		int getNumberOfChildren(){
			return nbChild -1;
		}
	}
	
	public enum CommentType{
		DEPLOY_TRACE (				Category.BATCH, 		"Deployment trace added by migration script"),
		DEPRECATED_REPLACED (		Category.DEPRECATED, 	"Deprecated function replaced by migration script - old function was : "),
		DEPRECATED_NON_REPLACED (	Category.DEPRECATED, 	"This function is deprecated, please replace by : "),
		USERDEFINED_REPLACED (		Category.USERDEFINED, 	"Code replaced by user-defined function during migration script - old code was : "),
		USERDEFINED_NON_REPLACED (	Category.USERDEFINED, 	"This code could be replaced by user-defined function : "),
		VARIABLES_LISTED (			Category.BATCH, 		"Variable could be replaced ? "),
		VARIABLES_REPLACED (		Category.BATCH, 		"Variable auto-replaced by migration script - old one was : "),
		VARIABLES_NON_REPLACED (	Category.BATCH, 		"A new variable could be used like : ");
		
		String comment;
		Category category;
		int id;
		CommentType(Category _category, String _comment){
			id = _category.addChild();
			comment = _comment;
			category = _category;
		}
		String getComment(){
			return comment;
		}
		int getId(){
			return id;
		}
		Category getCategory(){
			return category;
		}
		String getTag(){
			return TAG.replaceAll("#", category.getId()+""+id);			
		}
	}
	
	private final static String TAG = "[BATCH_#] ";
	public final static String LINE_RETURN 			= "\n";

	private final static String START_COMMENT = " /* ";
	private final static String END_COMMENT = " */ ";
	
	public static String getCategoryTags(Category category){
		String firstID = category.getId()+"0";
		String lastID = category.getId()+""+category.getNumberOfChildren();
		if ( category.getNumberOfChildren() >= 1 )
			return TAG.replaceAll("#", firstID)+" -> "+TAG.replaceAll("#", lastID);
		else
			return TAG.replaceAll("#", firstID);			
	}
	
	public static ArrayList<String> getAllTags(){
		ArrayList<String> list = new ArrayList<String>();
		for ( CommentType type : CommentType.values()){
			list.add(type.getTag()+ " : "+ type.getComment());				
		}
		
		return list;
	}
	
	public static String getComment(CommentType type, boolean lineReturn){
		return getComment(type, "", "", lineReturn);
	}
	
	public static String getComment(CommentType type, String after, boolean lineReturn){
		return getComment(type, "", after, lineReturn);
	}

	public static String getComment(CommentType type, String before, String after, boolean lineReturn){
		String batchID = type.getCategory().getId()+""+type.getId();
		String comment = START_COMMENT;
		comment += before;
		comment += TAG.replaceAll("#", batchID);
		comment += type.getComment();
		comment += after;
		comment += END_COMMENT;
		if ( lineReturn )
			comment += LINE_RETURN;
		return comment;
	}
}
